#!/usr/bin/env node

import path from 'path';
import pack from "./packag";
import unpack from "./unpack";
import loadrun from "./run/loadRunner";
import Path from './util/Path';
process.stdin.setEncoding('utf8');

// 运行类型 如打包，运行包，解压包
let runType = "";

// 运行文件
let runFile = "";
// 指定要运行的包
let runPackageName = "";
// 打包文件夹
let packageDir = "";
// 过滤文件文件夹
let filterFile = [];
// 过滤文件后缀，指定后缀的文件不打包
let filterSuffix = [];

let type = process.argv.splice(2);
type.length || help();


for(let e=0; e<type.length;e+=2){
    pars_param(type[e],type[e+1]);
}

switch (runType){
    // 运行包
    case "run":{
        loadrun.mainName = runPackageName;
        loadrun.run(runFile);
        break;
    }
    // 打包
    case "pack":{
        // 设置过滤的文件夹
        pack.libArray.push(...filterFile);
        pack.filterSuffix.push(...filterSuffix)
        // 过滤
        pack.start(packageDir);
        break;
    }
    // 解压包
    case "unpack":{
        unpack.reduction(runFile);
        break;
    }
}

// 解析命令台参数
function pars_param(x:string,y:string){
    switch (x){
        // 打包
        case "p":
        case "-p":
        case "pack":{
            runType = "pack"
            // 进行打包
            packageDir = y || process.cwd();
            break;
        }
        // 运行包名
        case "r":
        case "-r":
        case "run":{
            runType = "run";
            // 进行运行
            // if(!y.includes(".nar"))y+=".nar";
            runPackageName = y;
            break;
        }
        // 还原包
        case "u":
        case "-u":
        case "unpack":{
            runType = "unpack";
            // 解包，还原
            if(!y.includes(".nar"))y+=".nar";
            runFile = y;
            break;
        }
        // 添加过滤的文件夹
        case "f":
        case "-f":
        case "folder":{
            // 参数以逗号or竖线or;进行分割
            filterFile.push(...y.split(/[,|;|:]/).filter(e=>e).map(e=>{
                // e是绝对路径
                if(!e.startsWith(".")){
                    return Path.formatt(e,"/");
                }else{
                    return Path.formatt(path.join(process.cwd(),e),"/");
                }
            }))
            break;
        }
        case "s":
        case "-s":
        case "suffix":{
            filterSuffix.push(...y.split(/[,|;]/).filter(e=>e));
            break;
        }
        case "h":
        case "-h":
        case "help":{
            help();
            break;
        }
        case "v":
        case "-v":
        case "version":{
            console.log("version: \""+require("./package.json").version+"\"  2021-12-18")
            break;
        }
        default:{
            if(x){
                runType = "run";
                if(!x.includes(".nar"))x+=".nar";
                // 默认就是运行
                runFile = x;
            }else{
                help();
            }
            
        }
    }
}

function help(){
    console.log(`
    nar 指令

    -p  [path]                                  进行打包 参数默认为./
    -r  [name]                                  运行指定的package包
    -u  [path]                                  还原打包
    -f  [param1:param2:...param]                打包过滤某些文件夹以,;:进行分割,如 ./public
    -s  [param1:param2:...param]                打包过滤某些后缀的文件以,;:进行分割,如 .d.ts
    -v                                          显示nar版本信息
    -h                                          显示帮助信息
    nar [path]                                  无指令表示执行默认的package包
                `)
}